package com.swsc.solarverse.service.forward.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swsc.solarverse.common.util.ObjectMapper;
import com.swsc.solarverse.dao.entity.forward.ForwardInverterDataDO;
import com.swsc.solarverse.dao.mapper.forward.ForwardInverterDataMapper;
import com.swsc.solarverse.model.StationGenerationPowerBO;
import com.swsc.solarverse.service.forward.IForwardInverterDataService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-08-24
 */
@Service
public class ForwardInverterDataServiceImpl extends ServiceImpl<ForwardInverterDataMapper, ForwardInverterDataDO> implements IForwardInverterDataService {
    @Override
    public void saveOrUpdateDos(List<ForwardInverterDataDO> dataDos) {
        if (CollectionUtils.isNotEmpty(dataDos)) {
            for (ForwardInverterDataDO dataDo : dataDos) {
                ForwardInverterDataDO rawDataDo = baseMapper.selectOne(Wrappers.lambdaQuery(ForwardInverterDataDO.class)
                        .eq(ForwardInverterDataDO::getTenantNo, dataDo.getTenantNo())
                        .eq(ForwardInverterDataDO::getOrganNo, dataDo.getOrganNo())
                        .eq(ForwardInverterDataDO::getDeviceId, dataDo.getDeviceId())
                        .orderByDesc(ForwardInverterDataDO::getDataTime)
                        .last("LIMIT 1")
                );
                if (null == rawDataDo) {
                    baseMapper.insert(dataDo);
                } else {
                    Integer id = rawDataDo.getId();
                    ObjectMapper.map(dataDo, rawDataDo);
                    rawDataDo.setId(id);
                    try {
                        baseMapper.updateById(rawDataDo);
                    } catch (Exception e) {
                        log.error("========{}", e);
                    }
                }
            }
        }
    }

    @Override
    public void updateDos(List<ForwardInverterDataDO> dataDos) {
        if (CollectionUtils.isNotEmpty(dataDos)) {
            for (ForwardInverterDataDO dataDo : dataDos) {
                baseMapper.update(dataDo, Wrappers.lambdaUpdate(ForwardInverterDataDO.class)
                        .eq(ForwardInverterDataDO::getTenantNo, dataDo.getTenantNo())
                        .eq(ForwardInverterDataDO::getOrganNo, dataDo.getOrganNo())
                        .eq(ForwardInverterDataDO::getDeviceId, dataDo.getDeviceId())
                );
            }
        }
    }

    @Override
    public StationGenerationPowerBO generationPower(Integer tenantNo, Integer organNo, LocalDateTime dataTimeStart, LocalDateTime dataTimeEnd) {
        return baseMapper.generationPower(tenantNo, organNo, dataTimeStart, dataTimeEnd);
    }

    @Override
    public void insertOrUpdateBatch(List<ForwardInverterDataDO> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            baseMapper.insertOrUpdateBatch(list);
        }

    }
}
